﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using JiGBot.Classes;
using System.Windows;
using System.Windows.Controls;

namespace JiGBot
{
    public class Util
    {
        public static Process Tibia;
        public static IntPtr Handle;
        public Window wnd;
        public ComboBox cb;
        Grid sp;
        /*
        Player _Player;
        Healer _Healer;
        Client _Client;
         */

        // Constructor
        public Util(Process Client)
        {
            Tibia = Client;
            Handle = Tibia.MainWindowHandle;
            #region Set Client Chooser window
            cb = new ComboBox();
            Thickness cbMargin = cb.Margin;
            cbMargin.Bottom = 35;
            cbMargin.Top = 10;
            cbMargin.Left = 10;
            cbMargin.Right = 10;
            cb.Margin = cbMargin;
            cb.Name = "cbClient";
            cb.Items.Add("Select a client");
            cb.SelectedIndex = 0;
            foreach (Process p in GetClients())
            {
                cb.Items.Add(Convert.ToString(p.Id));
            }
            #endregion
        }

        // Destructor
        ~Util()
        {

        }

        public void ChooseClient()
        {
            Button btn = new Button();
            btn.Content = "Select";
            btn.Click += new RoutedEventHandler(btnSelectClient_Click);
            Thickness btnMargin = btn.Margin;
            btnMargin.Bottom = 10;
            btnMargin.Top = 35;
            btnMargin.Left = 10;
            btnMargin.Right = 10;
            btn.Margin = btnMargin;
            sp = new Grid();
            sp.Children.Add(cb);
            sp.Children.Add(btn);
            wnd = new Window()
            {
                Title = "Please select a client!",
                Height = 80,
                Width = 303,
                WindowStyle = WindowStyle.None,
                Content = sp,
            };
            wnd.Show();
        }

        public void btnSelectClient_Click(object sender, RoutedEventArgs e)
        {
            sp.Children.RemoveRange(0, 10);
            wnd.Hide();
            if (cb.SelectedItem.ToString() == "Select a client")
            {
                MessageBox.Show("No client selected.");
            }
            else
            {
                Tibia = GetClientByPid(Convert.ToInt32(cb.SelectedItem));
                Handle = Tibia.MainWindowHandle;
            }
        }

        #region GetClient Methods

        public static Process[] GetClients()
        {
            return Process.GetProcessesByName("tibia");
        }

        public static Process GetFirstClient()
        {
            return GetClients()[0];
        }

        public static Process GetClientByPid(int pid)
        {
            foreach (Process p in GetClients())
            {
                if (p.Id == pid)
                {
                    return p;
                }
            }
            return null;
        }

        #endregion

        public void UpdateWorld()
        {

        }
    }
}
